# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from .models import *
from .SipPro import *
from .SipUser import *
from .admin_ivr import *
from .mylicence import *
from .LocalExtension import *
from .PublicExtension import *
from .sysrestart import *
from .CallOutExtension import *
from .filePath import *
from .GroupDial import *
import xadmin

class ACLAdmin(object):
    list_display = ['IP','name']
    list_per_page = 10
    add_form_template = 'myform.html'
    change_form_template = 'myform.html'
    def valid_forms(self):
        data = str(self.form_obj.data)
        strData = str(self.form_obj.data).replace(" u'_save': [u''],", '').split(',')
        ip = strData[0].split(':')[2].replace("[u'", "").replace("']", "").strip()
        if len(ip.split('.')) <> 4:
            self.message_user(u'错误,IP格式错误(192.168.1.85/32)!', 'error')
        elif len(ip.split('/')) <> 2:
            self.message_user(u'错误,IP格式错误(192.168.1.85/32)!', 'error')
        else:
            urllist = str(self.request.get_full_path()).split('/')
            strsql = "select * from pbx_acl where IP='"+ip+"'"
            if len(urllist) == 6:
                strsql = "select * from pbx_acl where IP='" + ip + "' and id<>"+ urllist[3]
            l = len(ReadSql(strsql))
            if l==0:
                return self.form_obj.is_valid()
            else:
                self.message_user(u'错误,IP已存在!', 'error')

    def save_models(self):
        obj = self.new_obj
        name = obj.name
        ip = obj.IP
        self.new_obj.save()
        urllist = str(self.request.get_full_path()).split('/')
        strsql = "select * from pbx_acl"
        if len(urllist) == 6:
            strsql = "select * from pbx_acl where id<>"+urllist[3]
        aclList = ReadSql(strsql)
        strIP=""
        for i in aclList:
            strIP += '<node type="allow" cidr="'+i[2]+'"/>\r\n      '
        strIP += '<node type="allow" cidr="'+ip+'"/>'
        WriteAcl(strIP)
        r =Reloadacl()
        if r == -1:
            self.message_user(u'告警,IPPBX加载失败，请重启IPPBX或者手动加载!', 'warning')

    def delete_models(self, obj):
        obj.delete()
        aclList = ReadSql("select * from pbx_acl")
        strIP = ""
        for i in aclList:
            strIP += '<node type="allow" cidr="' + i[2] + '"/>\r\n'
        WriteAcl(strIP)
        r = Reloadacl()
        if r == -1:
            self.message_user(u'告警,IPPBX加载失败，请重启IPPBX或者手动加载!', 'warning')


def WriteAcl(strIP):
    info = ReadFile(acl_file_path())
    info = info.replace('{node}', strIP)
    WriteFile(config_file_patch() + "acl.conf.xml", info)



xadmin.site.register(SipPro,SipProAdmin)
xadmin.site.register(ACL,ACLAdmin)
xadmin.site.register(SipUser,SipUserAdmin)
xadmin.site.register(ivr,ivrAdmin)
xadmin.site.register(licence,LicenceAdmin)
xadmin.site.register(LocalExtension,LocalExtensionAdmin)
xadmin.site.register(PublicExtension,PublicExtensionAdmin)
xadmin.site.register(sysrestart,sysrestartAdmin)
xadmin.site.register(CallOutExtension,CallOutExtensionAdmin)
xadmin.site.register(group,GroupDialAdmin)
